/**
 *	@file	radio_music.c
 *	@brief	ラジオ　ミュージックデータ生成
 *	@author	Miyuki Iwasawa
 *	@date	07.10.19
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/wordset.h"
#include "system/pm_rtc.h"
#include "application/app_tool.h"
#include "itemtool/itemsym.h"
#include "itemtool/myitem.h"
#include "msgdata/msg.naix"

#include "savedata/zukanwork.h"
#include "radio_prog.h"
#include "msgdata/msg_pg_radio.h"
#include "msgdata/msg_radio_p_music.h"

enum{
 MUSIC_ID_MARCH,
 MUSIC_ID_SONG,
 MUSIC_ID_HOUEN,
 MUSIC_ID_SINOU,
 MUSIC_ID_BEST,
 MUSIC_ID_MAX,
};

#define MUSIC_BEST_BGM_MAX	(25)

typedef struct{
 int heapID;

 u8	zenkoku_zukan_f:1;	///全国図鑑フラグ
 u8	music_id:7;
 u8	msg_id;
 u8	week;	///<曜日
 u8	ptn;	///<再生パターン
 u8	seq;
 RTCDate ini_date;
 RTCDate date;

 STRBUF	*str_week;		///<ジョウト/カントー
 STRBUF	*str_msg[2];	///<メッセージ
}RADIO_MUSIC_WORK;

static void music_ResInit(PM_RADIO_MAN* man);
static void music_ResRelease(PM_RADIO_MAN* man);

static const u16 music_bgm_nrm[] = {
	RADIO_BGM_MARCH,
	RADIO_BGM_KOMORIUTA,
	RADIO_BGM_HOUEN,
	RADIO_BGM_SINOU,
};

static const u16 music_bgm_best[MUSIC_BEST_BGM_MAX]={
	SEQ_GS_P_TITLE,				//0
	SEQ_GS_P_OPENING_TITLE_G,	//1
	SEQ_GS_P_ENDING,			//2
	SEQ_GS_P_ENDING2,			//3
	SEQ_GS_P_SENKYO,			//4

	SEQ_GS_P_E_MINAKI,			//5
	SEQ_GS_P_IBUKI,				//6
	SEQ_GS_P_EYE_J_SHOUJO,		//7
	SEQ_GS_P_EYE_J_SHOUNEN,		//8
	SEQ_GS_P_EYE_J_AYASHII,		//9

	SEQ_GS_P_EYE_BOUZU,			//10
	SEQ_GS_P_EYE_MAIKO,			//11
	SEQ_GS_P_EYE_ROCKET,		//12
	SEQ_GS_P_EYE_K_SHOUJO,		//13
	SEQ_GS_P_EYE_K_SHOUNEN,		//14

	SEQ_GS_P_EYE_K_AYASHII,		//15
	SEQ_GS_P_VS_ROCKET,			//16

	SEQ_GS_P_AIKOTOBA,			//17
	SEQ_GS_P_RADIO_JINGLE,		//18
	SEQ_GS_P_OHKIDO_RABO,		//19

	RADIO_BGM_MARCH_GB,			//20
	RADIO_BGM_KOMORIUTA_GB,		//21
	RADIO_BGM_HUE_GB,			//22
	SEQ_GS_P_POKEMON_THEME,		//23
	SEQ_GS_P_OHKIDO,			//24
};

static void radiosub_bgm_play(PM_RADIO_MAN* man, u8 music_id )
{
	u16 snd;
	RADIO_MUSIC_WORK* wk = man->ch_wk;

	if(music_id == MUSIC_ID_BEST){
		music_id = (gf_rand()%(MUSIC_BEST_BGM_MAX*1000))/1000;
		snd = music_bgm_best[music_id];
	}else{

		snd = music_bgm_nrm[music_id];
	}
	wk->music_id = music_id;
	RADIO_PLAY(snd);
}

/**
 *	@brief	ラジオ　ミュージックチャンネル　初期化
 *
 *	@param	lv	受信レベル PM_RADIO_RECLV型(RADIO_REC_LV1：弱、RADIO_REC_LV2：強)
 */
int RadioMusic_Init(PM_RADIO_MAN* man)
{
	RTCTime ntime;
	RADIO_MUSIC_WORK* wk = NULL;

	wk = sys_AllocMemory(man->heapID,sizeof(RADIO_MUSIC_WORK));
	MI_CpuClear8(wk,sizeof(RADIO_MUSIC_WORK));

	//全国図鑑フラグ取得
	wk->zenkoku_zukan_f = ZukanWork_GetZenkokuZukanFlag(SaveData_GetZukanWork(man->save));

	//曜日取得
	GF_RTC_GetDateTime(&wk->ini_date, &ntime);
	wk->week = wk->ini_date.week;
	wk->msg_id = wk->week+m_music_02_01;

	//再生パターン
	switch(wk->week){
	case RTC_WEEK_SUNDAY:	//名曲アルバム
		if(MyItem_CheckItem( SaveData_GetMyItem(man->save), ITEM_gbPUREIYAA, 1, wk->heapID )){
			wk->ptn = MUSIC_ID_BEST;
		}else{
			wk->ptn = MUSIC_ID_MARCH;
			wk->msg_id = m_music_02_10;
		}
		break;
	case RTC_WEEK_MONDAY:
	case RTC_WEEK_FRIDAY:
		wk->ptn = MUSIC_ID_MARCH;
		break;
	case RTC_WEEK_WEDNESDAY:
		if(wk->zenkoku_zukan_f){
			wk->ptn = MUSIC_ID_HOUEN;
			wk->msg_id = m_music_02_08;
		}else{
			wk->ptn = MUSIC_ID_MARCH;
		}
		break;
	case RTC_WEEK_TUESDAY:
	case RTC_WEEK_SATURDAY:
		wk->ptn = MUSIC_ID_SONG;
		break;
	case RTC_WEEK_THURSDAY:
		if(wk->zenkoku_zukan_f){
			wk->ptn = MUSIC_ID_SINOU;
			wk->msg_id = m_music_02_09;
		}else{
			wk->ptn = MUSIC_ID_SONG;
		}
		break;
	}
	man->ch_wk = (void*)wk;
	man->msg_no = 0;
	music_ResInit(man);
	Snd_BgmStop(Snd_NowBgmNoGet(),0);

	//再生開始
	radiosub_bgm_play(man,wk->ptn);

	//初回メッセージ表示
	man->fst_f = 0;
	return 0;
}

/**
 *	@brief	ラジオ　ミュージックチャンネル 解放
 */
int RadioMusic_End(PM_RADIO_MAN* man)
{
//	Snd_BgmStop(Snd_NowBgmNoGet(),1);

	music_ResRelease(man);

	MI_CpuClear8(man->ch_wk,sizeof(RADIO_MUSIC_WORK));
	sys_FreeMemoryEz(man->ch_wk);
	man->ch_wk = NULL;

	return 0;
}

int RadioMusic_Play(PM_RADIO_MAN* man)
{
	RADIO_MUSIC_WORK* wk = (RADIO_MUSIC_WORK*)man->ch_wk;

	switch(wk->seq){
	case 0:	//タイトルコール
		RadioSub_SentencePlay(man,m_music_01);
#ifdef PM_DEBUG
		if(sys.cont & PAD_BUTTON_L){
			STRBUF* buf = STRBUF_Create(4,man->heapID);
			STRBUF_SetNumber(buf,wk->music_id,2,NUMBER_DISPTYPE_ZERO,NUMBER_CODETYPE_HANKAKU);
			GF_STR_PrintColor(man->win_dj,FONT_SYSTEM,buf,40,0,MSG_ALLPUT,GF_PRINTCOLOR_MAKE(1,2,0),NULL);
			GF_BGL_BmpWinOn(man->win_dj);
			STRBUF_Delete(buf);
		}
#endif
		wk->seq++;
		break;
	case 1:
		if(!RadioSub_SentencePlayWait(man)){
			break;
		}
		wk->seq++;
		break;
	case 2:
		GF_RTC_GetDate(&wk->date);
		RadioSub_SentencePlay(man,wk->msg_id);
		wk->seq++;
		break;
	case 3:
		if(!RadioSub_SentencePlayWait(man)){
			break;
		}
		{
			RTCDate	now;
			GF_RTC_GetDate(&now);

			if(now.day == wk->date.day && now.day == wk->ini_date.day){
				wk->seq = 2;	//繰り返し
				return 0;
			}
			//再抽選
			man->program_end_f = TRUE;
			Snd_BgmStop(Snd_NowBgmNoGet(),30);
			wk->seq++;

		}
		break;
	case 4:
		if(!RadioSub_MsgWaitScroll(man)){
			break;
		}
		return 1;
	}
	return 0;
}

/*
 *	@brief	ミュージックチャンネル用 必要なデータを初期化する
 */
static void music_ResInit(PM_RADIO_MAN* man)
{
	u32	len;
	STRBUF* tmp,*buf,*week;
	RADIO_MUSIC_WORK* wk = (RADIO_MUSIC_WORK*)man->ch_wk;

	man->msg_prog = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,NARC_msg_radio_p_music_dat,man->heapID);

	//番組タイトルとDJ名を通知
	MSGMAN_GetString(man->msg_prog,m_music_title,man->str_title);
	MSGMAN_GetString(man->msg_prog,m_music_dj,man->str_dj);
}

/**
 *	@brief	ミュージックチャンネル用　リソース解放
 */
static void music_ResRelease(PM_RADIO_MAN* man)
{
	RADIO_MUSIC_WORK* wk = (RADIO_MUSIC_WORK*)man->ch_wk;
	MSGMAN_Delete(man->msg_prog);
}
